package br.com.erpcortume.DAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.erpcortume.beans.Adiantamento;
import br.com.erpcortume.beans.Funcionario;
import br.com.erpcortume.repositorio.AdiantamentoRepositorio;

@Repository
@Transactional
public class AdiantamentoDAO implements AdiantamentoRepositorio{

	private SessionFactory sf;
	
	@Autowired
	public AdiantamentoDAO(SessionFactory factory) {
		this.sf = factory;
	}
	
	private Session getSession(){
		return sf.getCurrentSession();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Adiantamento> getBySalario(Funcionario funcionario) {
		
		Criteria crit = getSession().createCriteria(Adiantamento.class);
		
		crit.add(Restrictions.eq("funcionario.id", funcionario.getId()));

		return crit.list(); 
	}
	
	
	public Adiantamento getLastInserted(){
		Criteria crit = getSession().createCriteria(Adiantamento.class);

		crit.addOrder(Order.desc("id"));
		
		return (Adiantamento) crit.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Adiantamento> listAdiantamentos() {
		Criteria crit = getSession().createCriteria(Adiantamento.class);
		
		crit.addOrder(Order.desc("id"));
		
		return crit.list();
	}

}
